/* Jeff Snell - PROG38263
*  count.c - This program will read in a block of text (up to 5000 characters) 
*  and count the number of characters, words, and paragraphs in the text */

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include "count.h"

/*  **************************************************************
FUNCTION NAME: read_text
PURPOSE: gets characters from standard input until either an end of 
file or 5000 characters have been entered
INPUT: storage - the character string to store the text
OUTPUT: an integer which is the number of characters read
AUTHOR: Jeff Snell
**************************************************************** */
int read_text(char *storage) 
{
	int ch = 0;
	int i = 0;

	/* Received input until ctrl-d is hit (Linux), ctrl-z (windows) or MAX_CHARS has been reached. */
	while ( ( ch=getchar() ) != EOF && i < MAX_CHARS ) {
		storage[i] = ch;
		i++;
	}
	
	/* Make sure the string is properly terminated. */
	if (i < MAX_CHARS)
		storage[i] = '\0';
	else
		storage[MAX_CHARS-1] = '\0';
	
	return strlen(storage);
}

/***************************************************************
FUNCTION NAME: count
PURPOSE: counts characters, words, and paragraphs in the input text
INPUT: storage - the character string holding the text
length  - the length of the string
cords - a structure used to go to a specific coordinate on
the screen
OUTPUT: counts are written to stdout. Function returns nothing
AUTHOR: Jeff Snell
**************************************************************** */
void count(char *storage, int length)
{
	score scorecount;
	int i;

	scorecount.char_count = 0;
	scorecount.word_count = 0;
	scorecount.par_count = 0;

	for (i = 0; i < length; i++)
	{
		/* Counts the amount of characters excluding white space */
		if(storage[i] != ' ' && storage[i] != '\n' && storage[i] != '\t')
			scorecount.char_count++;

		/* Counts the amount of words, which is determined by a space or a new line character */
		if(storage[i] == ' ' || storage[i] == '\n')
			scorecount.word_count++;

		/* Check for 3 special cases in which the above count will fail. ' ' next to a '\n'*/
		if(storage[i] == ' ' && storage[i+1] == '\n')
			scorecount.word_count--;

		/* ' ' next to a ' ' */
		if(storage[i] == ' ' && storage[i+1] == ' ')
			scorecount.word_count--;

		/* and '\n' next to '\n' */
		if(storage[i] == '\n' && storage[i+1] == '\n')
			scorecount.word_count--;

		/* Counts the amount of paragraphs, which is determined by a new line character */
		if(storage[i] == '\n')
			scorecount.par_count++;
		
		if((storage[i] == '\n') && (storage[i+1] == '\n'))
			scorecount.par_count--;
	}

	printf("\n+----------------COUNT---------------+\n");
	printf("# of characters: %d\n", scorecount.char_count);
	printf("# of words: %d\n", scorecount.word_count);
	printf("# of paragraphs: %d\n", scorecount.par_count);
}

/***************************************************************
FUNCTION NAME: main for count.c
PURPOSE: calls read_text, prints a header, then calls count to 
Print the counts
INPUT: none
OUTPUT: none
AUTHOR: Jeff
**************************************************************** */
int main()
{
	char textBlock[MAX_CHARS];
	int length = 0;

	printf("Assignment 4 Question 3 - Jeff Snell\nType text and hit ctrl-D or command-D to stop.\n");
	length = read_text(textBlock);
	count(textBlock, length);
	
	return 0;
}
